package com.demo2do.fms.admin.service.supplier.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.supplier.SupplierDao;
import com.demo2do.fms.admin.dao.supplier.SupplierRelationDao;
import com.demo2do.fms.admin.dao.supplier.TeamDao;
import com.demo2do.fms.admin.entity.search.supplier.TeamSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.TeamService;
import com.demo2do.fms.entity.driver.Team;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * @author Wy
 */
@Service("teamService")
public class TeamServiceImpl implements TeamService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dao
    private TeamDao teamDao;

    @Dao
    private SupplierDao supplierDao;
    
    @Dao
    private SupplierRelationDao supplierRelationDao;
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#countTeam(com.demo2do.fms.admin.entity.security.Login)
     */
    public int count(Login login) {
        
        if (!login.isSupplier()) {  // 所有车队
            
            return teamDao.count();
            
        } else {                    // 供方
            
            Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
            return teamDao.count(supplier);
            
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#count(com.demo2do.core.persistence.support.QueryFilter)
     */
    public int count(TeamSearch search, Login login) {
        
        if (login.isSupplier()) {
            search.setSupplierId(login.getSupplierId());
        }
        
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#countSubTeam(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public int countSub(Login login, Page page) {
        
        if (!login.isSupplier()) {  // 查询所有车队
            
            return teamDao.count();
            
        } else {        // 一级供方
            
        	Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<Long> supplierIds = supplierRelationDao.listSponsoredSupplierId(supplier);
            
            if (CollectionUtils.isEmpty(supplierIds)) {
                return 0;
            }
            return teamDao.count(supplierIds);
            
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#countSubTeam(com.demo2do.fms.admin.entity.search.supplier.TeamSearch, com.demo2do.fms.admin.entity.security.Login)
     */
    public int countSub(TeamSearch search, Login login) {
        
        if (login.isSupplier()) {
        	
        	Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<Long> supplierIds = supplierRelationDao.listSponsoredSupplierId(supplier);
            
            if (CollectionUtils.isEmpty(supplierIds)) {
            	return 0;
            }
            search.addSupplierIds(supplierIds);
            
        }
        
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        return genericDaoSupport.count(sentence, parameters);
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#listTeam(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<Team> list(Login login, Page page) {
        
        page.calculate();
        
        if (!login.isSupplier()) {      // 非供方用户
            
            return teamDao.list(page.getBeginIndex(), page.getEveryPage());
            
        } else {                        // 供方

            Supplier supplier = supplierDao.loadSupplier(login.getSupplierId());
            return teamDao.list(supplier, page.getBeginIndex(), page.getEveryPage());
            
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#countLogin(com.demo2do.fms.admin.entity.security.Login,com.demo2do.core.entity.page.Page)
     */
    public List<Team> listSub(Login login, Page page) {

        page.calculate();
        
        if (!login.isSupplier()) {      // 非供方用户
            
            return teamDao.list(page.getBeginIndex(), page.getEveryPage());
            
        } else {                        // 一级供方

        	Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<Long> supplierIds = supplierRelationDao.listSponsoredSupplierId(supplier);
            return teamDao.list(supplierIds, page.getBeginIndex(), page.getEveryPage());
            
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#search(com.demo2do.fms.admin.entity.search.supplier.TeamSearch, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Team> search(TeamSearch search, Login login, Page page) {
        
        if (login.isSupplier()) {
            search.setSupplierId(login.getSupplierId());
        }

        // 1. build sentence
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 2. do query
        List<Team> teams = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        
        return teams;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#searchSubTeam(com.demo2do.fms.admin.entity.search.supplier.TeamSearch, com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Team> searchSub(TeamSearch search, Login login, Page page) {
        
        if (login.isSupplier()) {
        	Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<Long> supplierIds = supplierRelationDao.listSponsoredSupplierId(supplier);
            search.addSupplierIds(supplierIds);
        }

        // 1. build sentence
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        
        // 2. do query
        List<Team> teams = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        
        return teams;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#load(java.lang.Long)
     */
    public Team load(Long id) {
        return genericDaoSupport.load(Team.class, id);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#exist(java.lang.String)
     */
    public boolean exist(String code) {
        return teamDao.load(code) != null;
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#create(com.demo2do.fms.entity.driver.Team, java.lang.Long)
     */
    public void create(Team team, Login login) {
        
        // 0. check code exist
        Team existTeam = teamDao.load(team.getCode());
        if (existTeam != null) {
            throw BusinessException.newInstance("500", "该车队代码已存在，创建失败!");
        }
        
        // 1. prepare data
        Long operator = login.getId();
        Long supplierId = login.getSupplierId();
        Supplier supplier = supplierDao.loadSupplier(supplierId);
        Long organizationId = supplier != null ? supplier.getOrgId() : null;
        
        
        // 2. save team to database
        genericDaoSupport.save(team.initialize(supplier, organizationId, operator));
        
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#edit(com.demo2do.fms.entity.driver.Team, java.lang.Long)
     */
    public void edit(Team team, Long operator) {
        genericDaoSupport.update(team.operate(operator));
    }

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.TeamService#disable(java.lang.Long, java.lang.Long)
     */
    public void disable(Long id, Long operator) {
        Team team = genericDaoSupport.load(Team.class, id);
        genericDaoSupport.update(team.disable(true, operator));
    }
    
}